import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

import org.jfree.chart.ChartPanel;


public class TelaGrafico {

	private JFrame frmGraficoDoFeriado;
	
	private JTextField textNomeGrafico;
	
	private JComboBox<String> boxEixoX;
	private JComboBox<String> boxEixoY;
	
	private JCheckBox checkLegenda;
	
	private JButton botaoGerar;

	private static JPanel panelGrafico;
	
	public static String CAMINHO_TXT = "src/teste.txt";

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TelaGrafico window = new TelaGrafico();
					window.frmGraficoDoFeriado.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}//main
	
	/**
	 * Create the application.
	 */
	public TelaGrafico() {
		initialize();
	}//TelaGrafico()

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmGraficoDoFeriado = new JFrame();
		frmGraficoDoFeriado.setResizable(false);
		frmGraficoDoFeriado.setTitle("Gr\u00E1fico do Feriado");
		frmGraficoDoFeriado.setBounds(100, 100, 1000, 547);
		frmGraficoDoFeriado.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		frmGraficoDoFeriado.getContentPane().add(panel, BorderLayout.WEST);
		
		JPanel panel_1 = new JPanel();
		frmGraficoDoFeriado.getContentPane().add(panel_1, BorderLayout.NORTH);
		
		JPanel panel_2 = new JPanel();
		frmGraficoDoFeriado.getContentPane().add(panel_2, BorderLayout.SOUTH);
		
		JPanel panel_3 = new JPanel();
		frmGraficoDoFeriado.getContentPane().add(panel_3, BorderLayout.EAST);
		
		JPanel panel_4 = new JPanel();
		frmGraficoDoFeriado.getContentPane().add(panel_4, BorderLayout.CENTER);
		panel_4.setLayout(null);
		
		JLabel labelGraficoFeriado = new JLabel("GR\u00C1FICO DO FERIADO");
		labelGraficoFeriado.setFont(new Font("Tahoma", Font.BOLD, 20));
		labelGraficoFeriado.setHorizontalAlignment(SwingConstants.CENTER);
		labelGraficoFeriado.setBounds(6, 6, 968, 25);
		panel_4.add(labelGraficoFeriado);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(0, 43, 974, 6);
		panel_4.add(separator);
		
		JPanel panelInfoGrafico = new JPanel();
		panelInfoGrafico.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelInfoGrafico.setBounds(0, 61, 255, 229);
		panel_4.add(panelInfoGrafico);
		panelInfoGrafico.setLayout(null);
		
		JLabel labelNome = new JLabel("Nome do Gr\u00E1fico:");
		labelNome.setBounds(18, 17, 141, 20);
		panelInfoGrafico.add(labelNome);
		labelNome.setFont(new Font("Tahoma", Font.BOLD, 16));
		
		textNomeGrafico = new JTextField();
		textNomeGrafico.setBounds(18, 48, 221, 28);
		panelInfoGrafico.add(textNomeGrafico);
		textNomeGrafico.setColumns(10);
		
		JLabel labelEixoX = new JLabel("Eixo X:");
		labelEixoX.setBounds(18, 87, 57, 20);
		panelInfoGrafico.add(labelEixoX);
		labelEixoX.setFont(new Font("Tahoma", Font.BOLD, 16));
		
		ArrayList <String> tituloColunas = Graficos.getTituloColuna(CAMINHO_TXT); 
		
		boxEixoX = new JComboBox<String>();
		boxEixoX.setBounds(83, 85, 156, 26);
		panelInfoGrafico.add(boxEixoX);
		
		JLabel labelEixoY = new JLabel("Eixo Y:");
		labelEixoY.setBounds(18, 124, 57, 20);
		panelInfoGrafico.add(labelEixoY);
		labelEixoY.setFont(new Font("Tahoma", Font.BOLD, 16));
		
		boxEixoY = new JComboBox<String>();
		boxEixoY.setBounds(83, 122, 156, 26);
		panelInfoGrafico.add(boxEixoY);

		for (String titulo : tituloColunas) {
			boxEixoX.addItem(titulo);
			boxEixoY.addItem(titulo);
		}
		
		checkLegenda = new JCheckBox("Legenda");
		checkLegenda.setBounds(18, 176, 221, 18);
		panelInfoGrafico.add(checkLegenda);
		checkLegenda.setHorizontalAlignment(SwingConstants.CENTER);
		checkLegenda.setFont(new Font("Tahoma", Font.BOLD, 16));
		
		panelGrafico = new JPanel();
		panelGrafico.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelGrafico.setBounds(267, 61, 701, 435);
		panel_4.add(panelGrafico);
		
		botaoGerar = new JButton("GERAR");
		botaoGerar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ChartPanel myChartPanel = Graficos.plotarGrafico(CAMINHO_TXT, boxEixoX.getSelectedIndex(), boxEixoY.getSelectedIndex(), 
						textNomeGrafico.getText(), checkLegenda.isSelected(), true, true);
				myChartPanel.setSize(panelGrafico.getWidth(),panelGrafico.getHeight());
				myChartPanel.setVisible(true);
				panelGrafico.removeAll();
				panelGrafico.add(myChartPanel);
				panelGrafico.revalidate();
				panelGrafico.repaint();
			}
		});
		botaoGerar.setFont(new Font("Tahoma", Font.BOLD, 40));
		botaoGerar.setBounds(0, 302, 255, 194);
		panel_4.add(botaoGerar);
	}//initialize()
}
